package com.enation.app.shop.front.api.appcontroller;

import com.enation.app.base.core.model.Member;
import com.enation.app.base.core.service.IMemberManager;
import com.enation.app.base.core.upload.IUploader;
import com.enation.app.base.core.upload.UploadFacatory;
import com.enation.app.shop.core.member.model.MemberAddress;
import com.enation.app.shop.core.member.service.IFavoriteManager;
import com.enation.app.shop.core.member.service.IMemberAddressManager;
import com.enation.app.shop.front.api.credit.service.ICreditAccountManager;
import com.enation.eop.sdk.utils.StaticResourcesUtil;
import com.enation.framework.action.JsonResult;
import com.enation.framework.context.webcontext.ThreadContextHolder;
import com.enation.framework.util.FileUtil;
import com.enation.framework.util.JsonResultUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户类
 */
@Scope("prototype")
@Controller
@RequestMapping("/app/user")
public class UserAppController {
    @Autowired
    private IMemberManager memberManager;
    @Autowired
    private IMemberAddressManager memberAddressManager;
    @Autowired
    private IFavoriteManager favoriteManager;
    @Autowired
    private ICreditAccountManager creditAccountManager;

    private Logger logger = Logger.getLogger(getClass());
    /**
     *  //得到我的信息包括地址，收藏数
     * @param userid
     * @param jpushregisid
     * @return
     */
    @ResponseBody
    @RequestMapping("/getuserbyid")
    public JsonResult getUserById(int userid,String jpushregisid){
        // ThreadContextHolder.getHttpRequest().getSession().setAttribute("uid",userid);
        //new RedisClient().show();
       Member member= memberManager.getapp(userid);
       if (member==null){
           return JsonResultUtil.getErrorJson("没有该养殖场人员");
       }
       if(jpushregisid!=null){
          member.setJpushregisid(jpushregisid);
       }
       try {
          memberManager.edit(member);
       }catch (Exception e){
         logger.error("设置jpushid出错");
       }
       int collectcount=favoriteManager.getCount(userid);
        MemberAddress memberAddress= memberAddressManager.getMemberDefault(userid);
        Map map=new HashMap();
          try {
              map.put("addrid",memberAddress.getAddr_id());
              map.put("shipmobile",memberAddress.getMobile());
              map.put("shipname",memberAddress.getName());
              map.put("shipgenter",memberAddress.getGenter());
              map.put("shipaddr",memberAddress.getAddr());
              map.put("shipdefaddr",memberAddress.getDef_addr());
          }catch (Exception e){
              map.put("addrid",null);
              map.put("shipmobile",null);
              map.put("shipname",null);
              map.put("shipgenter",null);
              map.put("shipaddr",null);
              map.put("shipdefaddr",null);
          }
            map.put("userid",member.getMember_id());
            map.put("uname",member.getUname());
            map.put("name",member.getName());
           map.put("membertype",member.getMember_type());
            try {
                map.put("face", StaticResourcesUtil.transformPathapp(member.getFace()));
            }catch (Exception e){
                map.put("face", null);
            }
            map.put("point",member.getPoint());
            map.put("collectcount",collectcount);
        return  JsonResultUtil.getObjectMessageJson(map,"获取会员信息");
    }

    /**
     * //上传头像
     * @param file
     * @param userid
     * @return
     */
    @ResponseBody
    @RequestMapping("/uploadhead")
    public  JsonResult  uploadhead(MultipartFile file,int userid){

       Member member=memberManager.get(userid);
       if (member==null){
           return  JsonResultUtil.getErrorJson("没有该会员");
       }
        //先上传图片
        String faceField = "faceFile";

        if(file!=null){
            //判断文件类型
            if(!FileUtil.isAllowUpImg(file.getOriginalFilename())){
                return JsonResultUtil.getErrorJson("不允许上传的文件格式，请上传gif,jpg,bmp格式文件。");
            }
            //判断文件大小
            if(file.getSize() > 200 * 1024){
                return JsonResultUtil.getErrorJson("'对不起,图片不能大于200K！");

            }
            InputStream stream=null;
            try {
                stream=file.getInputStream();
            } catch (Exception e) {
                e.printStackTrace();
            }
            IUploader uploader= UploadFacatory.getUploaer();
            String imgPath=	uploader.upload(stream, faceField,file.getOriginalFilename());
            member.setFace(imgPath);

           Member member1=memberManager.edit(member);
            if (member1==null){
              return    JsonResultUtil.getErrorJson("修改头像失败");
            }
              return  JsonResultUtil.getObjectMessageJson(member1, "修改头像成功");

        }else {
                return JsonResultUtil.getErrorJson("没有文件");
            }
        }


    /**
     * 创建地址-需要request中 带地址参数
     //* @param shipName 收货人 【必填】
     //* @param shipTel 电话 【选填】
     //* @param shipMobile 手机 【选填】
     //* @param province_id 省份id 【必填】
     //* @param city_id 城市id 【必填】
     //* @param region_id 地区id 【必填
     //* @param province -- 【必填】
     // * @param city -- 【必填】
     //* @param region -- 【必填
     //* @param shipAddr 收获地址
     //* @param shipZip -- 【必填】
     * @return
     */
    @ResponseBody
    @RequestMapping("/createaddress")
    private MemberAddress createAddress(){
        HttpServletRequest request  = ThreadContextHolder.getHttpRequest();

        MemberAddress address = new MemberAddress();


        String name = request.getParameter("shipName");
        address.setName(name);

        String tel = request.getParameter("shipTel");
        address.setTel(tel);

        String mobile = request.getParameter("shipMobile");
        address.setMobile(mobile);

        String province_id = request.getParameter("province_id");
        if(province_id!=null){
            address.setProvince_id(Integer.valueOf(province_id));
        }

        String city_id = request.getParameter("city_id");
        if(city_id!=null){
            address.setCity_id(Integer.valueOf(city_id));
        }

        String region_id = request.getParameter("region_id");
        if(region_id!=null){
            address.setRegion_id(Integer.valueOf(region_id));
        }
        String town_id = request.getParameter("town_id");
        if(town_id!=null&&!town_id.equals("")){
            address.setTown_id(Integer.valueOf(town_id));
        }

        String province = request.getParameter("province");
        address.setProvince(province);

        String city = request.getParameter("city");
        address.setCity(city);

        String region = request.getParameter("region");
        address.setRegion(region);

        String town = request.getParameter("town");
        if(town!=null && !"".equals(town)){
            address.setTown(town);
        }

        String addr = request.getParameter("shipAddr");
        address.setAddr(addr);

        String zip = request.getParameter("shipZip");
        address.setZip(zip);

        memberAddressManager.addAddress(address);

        return address;
    }

}
